using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Cyys.Transition
{
    public class TransitionManager : Singleton<TransitionManager>
    {
        [Header("设置游戏开始场景")]
        public string startSceneName = string.Empty;


        public override void Awake()
        {
            base.Awake();
        }

        private IEnumerator Start()
        {
            // 等待一帧，确保所有组件都已初始化并完成OnEnable
            yield return null;

            EventHandler.CallStartNewGameEvent(1);
        }

        private void OnEnable()
        {
            // EventHandler.TransitonEvent += OnTransitionEvent;
            EventHandler.StartNewGameEvent += OnStartNewGameEvent;
            // EventHandler.EndGameEvent += OnEndGameEvent;
        }
        private void OnDisable()
        {
            // EventHandler.TransitonEvent -= OnTransitionEvent;
            EventHandler.StartNewGameEvent -= OnStartNewGameEvent;
            // EventHandler.EndGameEvent -= OnEndGameEvent;
        }

        private void OnStartNewGameEvent(int obj)
        {
            StartCoroutine(LoadSaveDataScene(startSceneName));
        }

        private void OnTransitionEvent(string sceneToGo, Vector3 positionToGo)
        {
            // if (!isFade)
            //     StartCoroutine(Transition(sceneToGo, positionToGo));
        }

        public IEnumerator LoadSaveDataScene(string sceneName)
        {
            // yield return Fade(1f);

            if (SceneManager.GetActiveScene().name != "PersistentScene")//在游戏过程中 加载另外游戏进度
            {
                EventHandler.CallBeforeSceneUnloadEvent();
                yield return SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene().buildIndex);
            }

            yield return LoadSceneSetActive(sceneName);
            EventHandler.CallAfterSceneLoadEvent();
            // yield return Fade(0);
        }


        /// <summary>
        /// 加载场景并设置为激活
        /// </summary>
        /// <param name="sceneName"></param>
        /// <returns></returns>
        private IEnumerator LoadSceneSetActive(string sceneName)
        {
            yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);

            Scene newScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);

            SceneManager.SetActiveScene(newScene);
        }
        
        /// <summary>
        /// 场景切换
        /// </summary>
        /// <param name="sceneName">目标位置</param>
        /// <param name="targetPosition">目标场景</param>
        /// <returns></returns>
        // private IEnumerator Transition(string sceneName, Vector3 targetPosition)
        // {
        //     // EventHandler.CallBeforeSceneUnloadEvent();
        //     // yield return Fade(1);

        //     // yield return SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene());// 卸载当前场景

        //     // yield return LoadSceneSetActive(sceneName);// 加载场景并设置为激活

        //     // //移动人物坐标
        //     // EventHandler.CallMoveToPosition(targetPosition);
        //     // EventHandler.CallAfterSceneLoadedEvent();
        //     // yield return Fade(0);
        // }
    }
}
